﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace BoilerplateWPF.Utils
{
    public class ObservableObject : INotifyPropertyChanged
    {
        // 固定写法,使属性改变后通知界面 自动获取属性名,允许无参调用
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false; // 值未变化

            field = value; // 赋值
            RaisePropertyChanged(propertyName); // 通知UI
            return true; // 值已变化
        }
    }
}
